package com.bianmin.coomen;


import com.bianmin.Data.Result;
import com.bianmin.Data.ResultCode;
import com.obs.services.ObsClient;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

public class UploadManager {

    private static  UploadManager instance;
    private UploadManager() {

    }
    private static ObsClient client;


    //初始化
    public static UploadManager getInstance() {
        if (instance == null) {
            instance = new UploadManager();
            createObsObj();
        }
        return instance;
    }

    //创建华为OBS对象
    private static void createObsObj() {
        String endPort = "obs.cn-east-3.myhuaweicloud.com";
        String ak = "ELSGPIVCFJICM3YKNPQZ";
        String sk = "y49vCmx7QdGAxtvElstVOnEoofBI9trj9a9rVQ5B";
        client = new ObsClient(ak,sk,endPort);
    }

    //上传图片
    public static Result<String> uploadImage(MultipartFile file) {
        //文件明
        String key = "image/" + Util.randomString(5) + String.valueOf(System.currentTimeMillis()) + ".png";
        String bucketName = "wfn53400";
        try {
            InputStream fileStream = file.getInputStream();
            client.putObject(bucketName, key, fileStream);
            return Result.success("https://wfn53400.obs.cn-east-3.myhuaweicloud.com/" + key);
        }catch (Exception e) {
            return Result.error(ResultCode.RC1002);
        }
    }

}
